package com.hiro.framework.tilegame.model.actor;

import java.awt.Point;
import java.util.Queue;

import com.egghiro.framework.j2se.game.core.model.actor.Actor2D;
import com.egghiro.framework.j2se.game.core.view.SImage;
import com.egghiro.framework.j2se.game.core.view.canvas.SGraphics;
import com.egghiro.framework.j2se.game.model.element.Index;
import com.egghiro.framework.j2se.util.IterateRemover;
import com.egghiro.framework.j2se.util.SLinkedList;

public abstract class Tile extends Actor2D {
    protected static final SImage TILE_IMG = SImage.createImage("/img/tile.png");// TODO property  final??
//    protected static final int LABEL_FONT_SIZE = 25;
//    
//    public static int getTileWidth(){
//        return TILE_IMG.getWidth();//TODO
//    }
//    
//    public static int getTileHeight(){
//        return TILE_IMG.getHeight();//TODO
//    }
//    
//    public static final int DEFAULT_TILE_WIDTH = 100;
//    public static final int DEFAULT_TILE_MARGIN = 6;

    protected static final int DEFAULT_MOVE_STEP = 12;// TODO property

    protected Index index;
    
    protected Queue<Point> wayPointQueue;

    protected int step = DEFAULT_MOVE_STEP;

    public int getI() {
        return index.getI();
    }

    public int getJ() {
        return index.getJ();
    }

    public Tile(int i, int j) {
        super(TILE_IMG.getWidth(), TILE_IMG.getHeight());// TODO
        this.index = new Index(i, j);
        this.wayPointQueue = new SLinkedList<Point>();
    }

    public void addWayPoint(Point wayPoint) {
        this.wayPointQueue.add(wayPoint);
    }

    public void addWayPoint(int x, int y) {
        this.addWayPoint(new Point(x, y));
    }
    
    public void act(IterateRemover itRemover) {
        this.act();
//        if (isAutoRemove) {
        if (this.isToRemove()) {
            itRemover.remove();
        }
//        }
    }
    
    protected abstract boolean isToRemove();

    public void act() {
        if (this.wayPointQueue.size() > 0) {
            Point targetPoint = this.wayPointQueue.peek();
            int moveX = targetPoint.x - getX();
            int moveY = targetPoint.y - getY();
            if (moveX >= step) {
                moveX = step;
            } else if (moveX <= -step) {
                moveX = -step;
            }
            if (moveY >= step) {
                moveY = step;
            } else if (moveY <= -step) {
                moveY = -step;
            }
            if (moveX != 0 || moveY != 0) {
                move(moveX, moveY);
            }

            if (isInPosition(targetPoint)) {
                this.wayPointQueue.poll();
            }
        }
    }
    
    protected boolean isInPosition(Point targetPoint) {
        boolean isInPosition = true;
        if (targetPoint != null) {
            if (getX() == targetPoint.x && getY() == targetPoint.y) {
                // isInPosition = true;
            } else {
                isInPosition = false;
            }
        }
        return isInPosition;
    }

    public boolean isInPosition() {
        return this.wayPointQueue.isEmpty();
    }
    
    public void paint(SGraphics graphics) {
        graphics.drawImage(TILE_IMG, getX(), getY());
    }
}
